<template>
  <div>
    <el-form label-position="right" label-width="150px" :model="writerForm" :rules="rules">
      <el-form-item label="数据库源：" prop="targetDatasourceId">
        <el-select v-model="writerForm.targetDatasourceId" filterable @change="wDsChange">
          <el-option v-for="item in wDsList" :key="item.id" :label="item.sourceName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否创建表：">
        <el-select v-model="writerForm.status" filterable @change="changeType">
          <el-option v-for="item in dictList('sf_bool')" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据库表名：" prop="tableId" v-show="writerForm.status == 'false'">
        <el-select v-model="writerForm.tableId" allow-create default-first-option filterable @change="rTbChange">
          <el-option v-for="item in wTbList" :key="item.id" :label="item.tableName" :value="item.id" />
        </el-select>
      </el-form-item>
      <div style="margin: 5px 0;" />
      <el-form-item label="表所有字段：" v-show="writerForm.status">
        <el-checkbox v-model="writerForm.checkAll" :indeterminate="writerForm.isIndeterminate"
          @change="wHandleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;" />
        <el-checkbox-group v-model="writerForm.columns" @change="wHandleCheckedChange">
          <el-checkbox v-for="c in wColumnList" :key="c.id" :label="c">{{ c.columnName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="前置sql语句：">
        <el-input v-model="writerForm.preSql" placeholder="前置sql在insert之前执行" type="textarea" />
      </el-form-item>
      <el-form-item label="postSql">
        <el-input v-model="writerForm.postSql" placeholder="多个用;分隔" type="textarea" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { listDataSource } from '@/api/metadata/sqlconsole'
import { getTableList } from '@/api/masterdata/masterdataTable'
import { fetchcolumnList } from '@/api/metadata/column'
import Bus from '../busWriter'
export default {
  name: 'TableWriter',
  props: ['readerInfo'],
  data() {
    return {
      jdbcDsQuery: {
        current: 1,
        size: 200,
        ascs: 'datasource_name'
      },
      wDsList: [],
      schemaList: [],
      wColumnList: [],
      columns: [],
      fromTableName: '',
      wTbList: [],
      dataSource: '',
      createTableName: '',
      writerForm: {
        sourceId: undefined,
        tableId: '',
        tableName: '',
        checkAll: false,
        isIndeterminate: true,
        tableSchema: '',
        status: 'false',
        sourceName: undefined,
        preSql: '',//前置sql语句
        postSql: '',
        columns: []
      },
      readerForm: this.getReaderData(),
      rules: {
        targetDatasourceId: [{ required: true, message: '数据库源为必填项', trigger: 'change' }],
        tableName: [{ required: true, message: 'this is required', trigger: 'change' }],

      },
      key: ''
    }
  },
  created() {
    this.getJdbcDs()
  },
  methods: {
    //获取列名
    rTbChange(id) {
      this.wTbList.forEach(item => {
        if (item.id == id) {
          this.writerForm.tableName = item.tableName
        }
      })
      fetchcolumnList({ sourceId: id }).then(res => {
        if (res.data.code === 0) {
          this.wColumnList = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    rest() {
      this.wColumnList = []
      this.wTbList = []
      this.writerForm = {
        sourceId: undefined,
        tableId: '',
        tableName: '',
        checkAll: false,
        isIndeterminate: true,
        tableSchema: '',
        status: 'false',
        sourceName: undefined,
        preSql: '',//前置sql语句
        postSql: '',
        columns: []
      }
    },
    //选择是否创建表
    changeType(e) {
      this.key = e
      console.log(this.readerInfo, 'readerInfo')
      if (e == 'true') {
        this.wColumnList = this.readerInfo.columns
        this.writerForm.tableId = this.readerInfo.tableId
        this.writerForm.tableName = this.readerInfo.tableName
      } else {
        getTableList({ sourceId: this.writerForm.targetDatasourceId }).then(res => {
          if (res.data.code === 200) {
            this.wTbList = res.data.data
          }
        })
        this.wColumnList = []
        // 获取可用表
        if (this.writerForm.tableId) {
          fetchcolumnList({ sourceId: this.writerForm.tableId }).then(res => {
            if (res.data.code === 0) {
              this.wColumnList = res.data.data
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }

      }
    },
    // 获取可用数据源
    getJdbcDs() {
      this.loading = true
      listDataSource().then(response => {
        this.wDsList = response.data.data
        this.loading = false
      })
    },
    // 获取表名
    getTables(type) {
      getTableList({ sourceId: this.writerForm.targetDatasourceId }).then(res => {
        if (res.data.code === 200) {
          this.wTbList = res.data.data
        }
      })
    },
    wDsChange(e) {
      // 清空
      this.writerForm.tables = []

      this.wDsList.find((item) => {
        if (item.id === e) {
          this.writerForm.sourceName = item.sourceName
          this.dataSource = item
        }
      })
      this.changeType(this.key)
      Bus.sourceId = e
      // this.$emit('selectDataSource', this.dataSource)
    },
    wHandleCheckAllChange(val) {
      this.writerForm.columns = val ? this.wColumnList : []
      this.writerForm.isIndeterminate = false
    },
    wHandleCheckedChange(value) {
      const checkedCount = value.length
      this.writerForm.checkAll = checkedCount === this.wColumnList.length
      this.writerForm.isIndeterminate = checkedCount > 0 && checkedCount < this.wColumnList.length
    },
    getData() {
      if (Bus.sourceId) {
        this.writerForm.sourceId = Bus.SourceId
      }
      return this.writerForm
    },
    getReaderData() {
      return this.$parent.getReaderData()
    },
    getTableName() {
      return this.fromTableName
    }
  }
}
</script>
